package com.genfu.reform.controller;

import java.util.Collection;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ParameterAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;

import com.genfu.reform.model.NavigationNode;
import com.genfu.reform.service.GenfuCommonService;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.Validateable;
import com.opensymphony.xwork2.ValidationAwareSupport;

@ParentPackage("genfu-rest")
@InterceptorRefs({ @InterceptorRef("genfuAuthentication"),
		@InterceptorRef("restDefaultStack") })
@Results({ @Result(name = "success", type = "redirectAction", params = {
		"actionName", "navigation-node" }) })
public class NavigationNodeController extends ValidationAwareSupport implements
		ModelDriven<Object>, Validateable, SessionAware, ServletRequestAware,
		ServletResponseAware, ParameterAware, Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private NavigationNode model = new NavigationNode();// 此处必须实例化一个对象,用来新建对象
	private Long id;
	private Collection<NavigationNode> list;
	private GenfuCommonService genfuCommonService;
	private Map<String, Object> session;
	private Map<String, String[]> parameters;
	private boolean verifyingOperates;

	public NavigationNodeController(GenfuCommonService theService) {
		this.genfuCommonService = theService;
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		System.out.println("Controller...setServletResponse..." + arg0);

	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		System.out.println("Controller...setServletRequest..." + arg0);
	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}

	@Override
	public void validate() {
		System.out.println("Controller...validate...");

	}

	@Override
	public Object getModel() {
		System.out.println("Controller...getModel...");
		if (model != null) {
			System.out.println("Controller...getModel...");
		}
		return (list != null ? list : model);
	}

	public void setModel(NavigationNode model) {
		System.out.println("Controller...setModel...");
		this.model = model;
	}

	public HttpHeaders show() {
		System.out.println("Controller...show...");
		// if (this.id != 0 && this.model.getId() == null) {
		// this.model = eventService.get(this.id);
		// }
		return new DefaultHttpHeaders("show");
	}

	// 首先验证权限
	public void prepareIndex() throws Exception {
		System.out.println("Controller...prepareindex...");
		verifyingOperates = genfuCommonService.verifyingOperates(parameters,
				session);
	}

	public HttpHeaders index() {

		if (verifyingOperates && this.parameters.containsKey("statusCode")) {
			list = genfuCommonService.searchList(NavigationNode.class,
					parameters);
		}
		return new DefaultHttpHeaders("index").disableCaching();
	}

	public String update() {
		System.out.println("Controller...update...");
		genfuCommonService.update(model);
		addActionMessage("Object updated successfully");
		return "success";
	}

	public void setId(Long id) {
		System.out.println("Controller...setId..." + id);
		if (id != null) {
			this.model = (NavigationNode) genfuCommonService.find(id,
					NavigationNode.class);
		}
		this.id = id;
	}

	public String edit() {
		System.out.println("Controller...edit...");

		return "edit";
	}

	public String editNew() {
		System.out.println("Controller...editNew...");
		return "editNew";
	}

	public HttpHeaders create() {
		System.out.println("Controller...create...");
		genfuCommonService.save(model);
		addActionMessage("New Object created successfully");
		return new DefaultHttpHeaders("index").setLocationId(model.getId());
		// return new
		// DefaultHttpHeaders("success").setLocationId(model.getId());
	}

	public String deleteConfirm() {
		System.out.println("Controller...deleteConfirm...");
		return "deleteConfirm";
	}

	public String destroy() {
		System.out.println("Controller...destroy...");
		genfuCommonService.remove(model);
		addActionMessage("Object removed successfully");
		return "success";
	}

	@Override
	public void prepare() throws Exception {

	}

	@Override
	public void setParameters(Map<String, String[]> arg0) {
		this.parameters = arg0;
	}
}
